package com.songcha.library_common.ui.view.compose

import android.content.Context
import android.os.Build
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Box
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import coil.ImageLoader
import coil.compose.AsyncImage
import coil.compose.AsyncImagePainter
import coil.request.ImageRequest


@Composable
fun ComposeAsyncImage(modifier: Modifier = Modifier, url:String?,
                      contentScale: ContentScale = ContentScale.Crop,onState:((AsyncImagePainter.State)->Unit)? = null) {
    if(url == null) {
        Box(
            modifier = modifier,
        )
        return
    }

    val request = ImageRequest.Builder(LocalContext.current)
        .data(url)
        .allowHardware(false)
        .build()

    AsyncImage(
        modifier = modifier,
        model = request,
        contentDescription = null,
        contentScale = contentScale,
        onState = onState
    )
}